﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace SIMS_GUI_Item
{
    class SIMS_GUI_Item_DataBase
    {
        public string connStr = @"Data Source=DESKTOP-3CMC9MB\SQLEXPRESS;Initial Catalog=SIMS GUI Item DataBase;Integrated Security=True;";
        /// <summary>
        /// 单个结果查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public object ExecScalar(string sql)
        {
            var conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                return comm.ExecuteScalar();
            }
            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
            }
            return null;
        }
        /// <summary>
        /// 多个结果查询
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public SqlDataReader ExecReader(string sql)
        {
            var conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                var dr = comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                return dr;
            }
            catch(Exception)
            {

            }
            finally
            {

            }
            return null;
        }
        /// <summary>
        /// 执行新增、删除、修改
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool ExecNonQuery(string sql)
        {
            var conn = new SqlConnection(connStr);
            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                return comm.ExecuteNonQuery() > 0;
            }
            catch(Exception)
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
